package com.ellricky.baselib.ui.student;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.ellricky.baselib.R;
import com.ellricky.baselib.entity.student.StudentApplyDetailEntity;
import com.ellricky.baselib.entity.student.StudentApplyEntity;
import com.ellricky.baselib.http.callback.ECallback;
import com.ellricky.baselib.ui.base.BaseDetailActivity;
import com.ellricky.baselib.util.GlideUtil;
import com.ellricky.baselib.widget.DetailItemView;
import com.flyco.roundview.RoundTextView;

import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.ellricky.baselib.http.Api.API_STUDENT_APPLY_DEAL;
import static com.ellricky.baselib.http.Api.API_STUDENT_APPLY_DETAIL;

public class StudentApplyDetailActivity extends BaseDetailActivity {
    private CircleImageView ivAvatar;
    private TextView tvName;
    private TextView tvClassName;

    private DetailItemView tvSchoolArea;
    private DetailItemView tvTel;

    private DetailItemView tvType;
    private DetailItemView tvDate;
    private DetailItemView tvReason;

//    private DetailItemView tvFzTeacher;
//    private DetailItemView tvConsultant;

    private LinearLayout presidentView;
    private DetailItemView tvAuditName;
    private DetailItemView tvAuditState;
    private DetailItemView tvAudiTime;

    private RoundTextView btnDeal;

    private StudentApplyEntity applyEntity;
//    private String role;

    private int resultCode = 0;

    @Override
    public int getContentLayout() {
        return R.layout.base_activity_stu_apply_detail;
    }

    @Override
    public void initUIView() {
        super.initUIView();
        mTvTitle.setText(R.string.base_apply_detail);

        ivAvatar = findViewById(R.id.ivAvatar);
        tvName = findViewById(R.id.tvName);
        tvClassName = findViewById(R.id.tvClassName);

        tvSchoolArea = findViewById(R.id.tvSchoolArea);
        tvTel = findViewById(R.id.tvTel);

        tvType = findViewById(R.id.tvType);
        tvDate = findViewById(R.id.tvDate);
        tvReason = findViewById(R.id.tvReason);

//        tvFzTeacher = findViewById(R.id.tvFzTeacher);
//        tvConsultant = findViewById(R.id.tvConsultant);

        presidentView = findViewById(R.id.view_president);
        tvAuditName = findViewById(R.id.tvPresident);
        tvAuditState = findViewById(R.id.tvDealState);
        tvAudiTime = findViewById(R.id.tvDealDate);

        btnDeal = findViewById(R.id.btnDeal);
        btnDeal.setOnClickListener(this);


        applyEntity = (StudentApplyEntity) getIntent().getSerializableExtra("stu_list_entity");
//        role = LocalCacheUtil.getCardType();

        if (applyEntity != null) {
            if (applyEntity.isBkApply()) {
                tvType.setKey(getString(R.string.base_key_course_type));
                tvDate.setKey(getString(R.string.base_bk_date));
                tvReason.setKey(getString(R.string.base_bk_reason));
            } else if (applyEntity.isTkApply()) {
                tvType.setKey(getString(R.string.base_tk_type));
                tvDate.setKey(getString(R.string.base_tk_date));
                tvReason.setKey(getString(R.string.base_tk_reason));
            }

            loadDetailData();
        }

    }

    @Override
    public void onClickView(View v) {
        super.onClickView(v);
        if (v == btnDeal) {
//            if (CARD_CONSULTANT.equalsIgnoreCase(role)) {
//                EAppUtil.jumpStuApplyAdvice(this, applyEntity.getId(), applyEntity.getApplyType());
//            } else if (CARD_PRESIDENT.equalsIgnoreCase(role) || CARD_TEACHER_MANAGER.equals(role)) {
            confirmDeal();
//            }
        }
    }

    private void confirmDeal() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", applyEntity.getId());
        paramMap.put("applyType", applyEntity.getApplyType());
        mPresenter.sendBackRequest(API_STUDENT_APPLY_DEAL, paramMap);
    }


    private void loadDetailData() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", applyEntity.getId());
        paramMap.put("applyType", applyEntity.getApplyType());

        mPresenter.loadDetail(API_STUDENT_APPLY_DETAIL, paramMap, new ECallback<StudentApplyDetailEntity>(this) {
            @Override
            public void onSuccess(StudentApplyDetailEntity data, String msg) {
                GlideUtil.into(StudentApplyDetailActivity.this, data.getHeadPic(), ivAvatar);
                tvName.setText(data.getName());
                tvClassName.setText(data.getClassName());

                tvSchoolArea.setValue(data.getAreaName());
                tvTel.setValue(data.getTel());

                tvType.setValue(data.getClassTypeName());
                tvDate.setValue(data.getMakeupDate());
                tvReason.setValue(data.getMakeUpReason());


//                tvFzTeacher.setValue(data.getTeacherName());
//                tvConsultant.setValue(data.getSuggestInfo());

                tvAuditName.setValue(data.getSchoolMangerName());
                tvAuditState.setValue(data.getApproveStatusName());
                tvAudiTime.setValue(data.getSchoolMangerDate());

                // 审批状态UI变化
                String approveStatus = data.getApproveStatus();
//                if (CARD_PRESIDENT.equals(role) || CARD_TEACHER_MANAGER.equals(role)) {
                // 执行校长&学管：2-执行校长待审批 3-执行校长已审批
                if ("3".equals(approveStatus)) {
                    presidentView.setVisibility(View.VISIBLE);
                    btnDeal.setVisibility(View.GONE);
                } else {
                    presidentView.setVisibility(View.GONE);
                    btnDeal.setVisibility(View.VISIBLE);
                    btnDeal.setText(R.string.base_confirm_deal);
                }
               /* } else {
                    // 课程顾问：1-未处理 2-执行校长待审批（课程顾问已审）
                    presidentView.setVisibility(View.VISIBLE);
                    if ("1".equals(approveStatus)) {
                        btnDeal.setVisibility(View.VISIBLE);
                        btnDeal.setText(R.string.base_go_to_deal);
                    } else {
                        btnDeal.setVisibility(View.GONE);
                    }


                }*/
            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        this.resultCode = resultCode;
        loadDetailData();
    }

    @Override
    public void onKeyBack(boolean isKeyBack) {
        if (resultCode > 0) {
            setResult(resultCode);
        }
        super.onKeyBack(isKeyBack);
    }
}